package com.jungle.mogo.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.jungle.mogo.entity.pojo.User;
import com.jungle.mogo.entity.vo.UserReqVO;
import com.jungle.mogo.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Jungle
 * @create 2024-10-16 10:39
 * mongodb操作
 */
@RestController
@RequestMapping("/mongo/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public List<User> userList() {
        return userService.list();
    }


    /**
     * 根据id查询
     */
    @GetMapping("/get/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.getById(id);
    }


    /**
     * 根据名称查询
     */
    @GetMapping("/get")
    public User getUserByName(@RequestParam String name) {
        return userService.getUserByName(name);
    }


    /**
     * 新增
     */
    @PostMapping("/save")
    public Long save(@RequestBody UserReqVO userReqVO) {
        User bean = BeanUtil.toBean(userReqVO, User.class, CopyOptions.create(User.class, true, "id"));
        userService.save(bean);
        return bean.getId();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Boolean delete(@PathVariable Long id) {
        return userService.deleteById(id);
    }

    /**
     * 更新
     */
    @PutMapping("/update")
    public Boolean updateById(@RequestBody UserReqVO userReqVO) {
        return userService.updateById(BeanUtil.toBean(userReqVO, User.class));
    }
}
